//
//  WBUserAccountViewModel.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/11.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBUserAccountViewModel.h"
#import "WBNetworkTool.h"

@interface WBUserAccountViewModel ()

@property (nonatomic , copy) NSString *filePath;

@end

@implementation WBUserAccountViewModel

- (BOOL)isLogin
{
    
    return self.accessToken == nil ? NO : YES;

}



- (NSString *)accessToken
{
    //token存在的时候，比较有没有过期 过期就返回Nil 没过期的 就返回用户的token， 如果token不存在 就返回NIl  token为nil的时候 都要重新登录 去请求新的token
    if(self.userAccount.access_token != nil)
    {
        if([self.userAccount.expires_date compare:[NSDate date]] == NSOrderedDescending ){
            
            return self.userAccount.access_token;
        
        }else
        {
        
            return nil;
        
        }
    
    
    }else
    {
        return nil;
    }
    
    
}




//实现单例
static WBUserAccountViewModel *_instance;
+ (instancetype)sharedWBUserAccountViewModel
{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        _instance = [[WBUserAccountViewModel alloc] init];
        _instance.userAccount = [_instance getUserAccount];
        
    });

    return _instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;

}




- (NSString *)filePath
{
    if(!_filePath)
    {
     _filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userAccount.archiver"];
    
    }

    return _filePath;
}


- (void)getUserTokenWithCode:(NSString *)code andFinishedBlock:(void (^)(BOOL))finishBlock
{
    [[WBNetworkTool sharedWBNetworkTool] getUserTokenWithCode:code andSuccess:^(NSDictionary *responseObject) {
        
        WBUserAccount *user = [WBUserAccount accountWithDictionary:responseObject];
        
        //请求成功 根据code去请求头像和昵称
        [self getUserInfoWithUserAccount:user andFinishedBlock:finishBlock];
        
    } andFailure:^(NSError *error) {
        finishBlock(NO);
        NSLog(@"请求失败%@",error);
        
    }];

}

- (void)getUserInfoWithUserAccount:(WBUserAccount *)userAccount andFinishedBlock:(void (^)(BOOL))finishBlock
{
    
    [[WBNetworkTool sharedWBNetworkTool] getUserInfoWithUserAccessToken:userAccount andSuccess:^(NSDictionary *responseObject) {
        
        //数据请求成功  给account添加两个字段 头像 和  昵称
        userAccount.screen_name = responseObject[@"screen_name"];
        userAccount.avatar_large = responseObject[@"avatar_large"];
    
        [self saveUserAccount:userAccount];
        
        //告诉控制器数据请求成功
        finishBlock(YES);
        
    } andFailure:^(NSError *error) {
        finishBlock(NO);
        NSLog(@"请求失败 %@",error);
        
    }];

}

//数据归档
- (void)saveUserAccount:(WBUserAccount *)userAccount
{
    self.userAccount = userAccount;
    [NSKeyedArchiver archiveRootObject:userAccount toFile:self.filePath];

}

//数据解档
- (WBUserAccount *)getUserAccount
{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:self.filePath];

}












@end
